#include <tsin/library/BulletCirclePlacer.hpp>

#include <tsin/runtime/BulletRuntime.hpp>
#include <tsin/util/Logger.hpp>

#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace tsin;

DynamicObject *BulletCirclePlacer::copy()
{
        return DynamicObject::copy(new BulletCirclePlacer);
}

void BulletCirclePlacer::init(float centerX, float centerY, float radius, int n, bool isClockWise, float start)
{
        this->x = centerX;
        this->y = centerY;
        this->radius = radius;
        this->interval = 2 * M_PI / n;
        this->start = start;
        this->isClockwise = isClockwise;
}

void BulletCirclePlacer::reinit()
{
        float arg1 = arguments[0]->getFloat();
        float arg2 = arguments[1]->getFloat();
        float arg3 = arguments[2]->getFloat();
        int   arg4 = arguments[3]->getInt();
        bool  arg5 = arguments[4]->getBool();
        float arg6 = arguments[5]->getFloatInRad();

        switch(nrArguments) {
        case 4:
                if (strcmp(desp, "NNNN") == 0)
                        init(arg1, arg2, arg3, arg4);
                break;
        case 5:
                if (strcmp(desp, "NNNNB") == 0)
                        init(arg1, arg2, arg3, arg4, arg5);
                break;
        case 6:
                if (strcmp(desp, "NNNNBN") == 0)
                        init(arg1, arg2, arg3, arg4, arg5, arg6);
                break;
        }
}

void BulletCirclePlacer::reset()
{
        count = 0;
}

void BulletCirclePlacer::place(BulletRuntime *bullet, void *)
{
        float angle = start + (isClockwise ? interval : - interval) * count;
        bullet->x = x - radius * sin(angle);
        bullet->y = y + radius * cos(angle);
        bullet->angle = angle;
        count ++;
}

DynamicObject *bindCirclePlacer()
{
        return new BulletCirclePlacer;
}
